import 'package:shared_preferences/shared_preferences.dart';

class TokenManager {
  String _token = '';
  static const String _tokenKey = 'enjoy_token';

  /// 创建单例
  static final TokenManager _instance = TokenManager._();
  TokenManager._();
  static TokenManager get instance => _instance;

  /// 初始化
  Future<void> init() async {
    final prefs = await SharedPreferences.getInstance();
    _token = prefs.getString(_tokenKey) ?? '';
  }

  /// 获取token
  String? getToken() {
    return _token;
  }

  /// 设置token
  Future<void> setToken(String token) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_tokenKey, token);
    _token = token;
  }

  /// 清除token
  Future<void> clearToken() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove(_tokenKey);
    _token = '';
  }
}